<?php

// TODO
// controllare autenticazione chiamata
// controllare uid con sessone

include_once 'model.php';
include_once 'KLogger.php';

if (!isset($logger))
    $logger = new KLogger("log/vocabolario.log", KLogger::DEBUG);

$TIMEOUT = 600000;

session_start();

// if (!isset($_SESSION)) {
// session_start();
// }
// $sid = session_id();

$errori = array();

$action = "";
if (isset($_REQUEST['action']))
    $action = $_REQUEST['action'];

$urlxml = "protected/config.xml";
$config = new SimpleXmlElement($urlxml, NULL, true);

$host = $config -> connection -> host;
$port = $config -> connection -> port;
$database = $config -> connection -> database;
$user = $config -> connection -> user;
$pass = $config -> connection -> password;

$logger -> LogDebug("CONTROLLER:");

if (isset($action)) {

    $model = new Model($host, $port, $database, $user, $pass);
    $filter = "";

    $logger -> LogDebug("ACTION:$action");
    switch ($action) {
        case 'login_page' :
            header("Location: login.php");
            break;
        // TODO non ancora implementata , usato check.php
//         case 'login' :
//             $user = $_REQUEST['user'];
//             $password = $_REQUEST['password'];
//             $uid = $model -> checkLogin($user, $password, $users);
//             //echo "UID:".$uid;die;
//             session_destroy();
//             session_start();
//             if ($uid) {
//                 $_SESSION['expire'] = time() + $TIMEOUT;
//                 $_SESSION['uid'] = $uid;
//                 $_SESSION['login_user'] = $user;
//                 header("Location: list.php");
//             } else {
//                 header("Location: login.php?error=login");
//             }
//             break;
        case 'logout' :
            session_unset();
            header("Location: login.php");
            break;
        case 'vocabolario' :
            header("Location: vocabolario.php");
            break;
        case 'error' :
            header("Location: error.php");
            break;
        case 'insert' :
            $parola = html_entity_decode($_REQUEST['parola']);
            $definizione = html_entity_decode($_REQUEST['definizione']);
            $uid = html_entity_decode($_REQUEST['uid']);
            $res = $model -> insert($parola, $definizione, $uid);
            header("Location: vocabolario.php");
            break;
        case 'edit' :
            $id = html_entity_decode($_REQUEST['id']);
            $parola = html_entity_decode($_REQUEST['parola']);
            $definizione = html_entity_decode($_REQUEST['definizione']);
            $uid = html_entity_decode($_REQUEST['uid']);
            $res = $model -> edit($id, $parola, $definizione, $uid);
            header("Location: vocabolario.php");
            break;
        case 'clear' :
            // pulisce sessione
            session_destroy();
            header("Location: login.php");
            break;
        case 'delete' :
            if (isset($_REQUEST['id'])) {
                $id = $_REQUEST['id'];
                $uid = $_SESSION['uid'];
                $res = $model -> delete($id, $uid);
                header("Location: vocabolario.php");
            } else {
                header("Location: error.php?error=Errore id mancante");
            }
            break;
        case 'gentest' :
            // crea test
            if (isset($_REQUEST['num'])) {
                $num = intval($_REQUEST['num']);
                $uid = intval($_SESSION['uid']);
                $arr = $model -> genTest($num, $uid);
                $_SESSION['test'] = $arr;
                header( "Location: test.php" );
            }
            break;
        case 'checktest' :
            $id = intval($_REQUEST['id']);
            $val = $_REQUEST['val'];
            $res = $model -> checkTest($id, $val);
            echo json_encode($res);
            break;
        default :
            header("Location: error.php?error=action");
            break;
    }
    $model -> close();
    return;
} else
    header("Location: error.php");
?>